package cn.wolfcode.luowowo.web.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.redis.domian.StrategyStatisVO;
import cn.wolfcode.luowowo.redis.service.IStrategyStatisVoRedisService;
import cn.wolfcode.luowowo.redis.util.BeanUtil;
import cn.wolfcode.luowowo.redis.util.RedisKeys;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 实现一个容器上下文对象,告诉容器要监听谁
 * ContextRefreshedEvent  容器启动时监听
 */
//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        System.out.println("============redis数据初始化开始========================");
        //数据初始化
        List<StrategyDetail> listRedis = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : listRedis) {
            //如果第一次初始化了数据为100, 之后前端操作变成200,如果突然重启服务器就变成了100了
            //所以要判断key是否存在,如果存在就跳过当前覆盖
            if(strategyStatisVoRedisService.isExistStrategyVoKey(detail.getId())){
                continue;
            }
            vo = new StrategyStatisVO();
            //数据初始化,可以覆盖
            BeanUtil.copyProperties(vo,detail);
            vo.setStrategyId(detail.getId());
            vo.setDestName(detail.getDest().getName());
            vo.setTitle(detail.getTitle());
            vo.setDestId(detail.getDest().getId());
            //同步到redis中
            strategyStatisVoRedisService.setStrategyStatisVo(vo);
        }
        System.out.println("============redis数据初始化结束========================");


        /**
         * 如果不判断一下第二次重启的时候会将原来原有的分数继续加进去
         */
        System.out.println("============hot推荐排行榜推荐开始================");

        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {

            if (strategyStatisVoRedisService.isZsetStatisVoExeist(RedisKeys.STRATEGY_ZSET_SORT_HOT.getPrefix(),detail.getId())){
                    continue;
            }
            //访问数+评论数
            int score = detail.getViewnum() + detail.getReplynum();
            strategyStatisVoRedisService.addHotScore(detail.getId(),score);
        }

        System.out.println("============hot推荐排行榜推荐结束================");

        System.out.println("============国内外推荐排行榜开始================");

        for (StrategyDetail detail : list) {
            if (strategyStatisVoRedisService.isZsetStatisVoExeist(RedisKeys.STRATEGY_ZSET_SORT_ISABROAD.getPrefix(),detail.getId())){
                continue;
            }
            //点赞数 + 收藏数
            int score = detail.getThumbsupnum() + detail.getFavornum();
            strategyStatisVoRedisService.addIsAbroadCommend(detail.getId(),score);
        }

        System.out.println("============国内外推荐排行榜结束================");

    }
}
